package cn.org.donald.service.impl;

import cn.org.donald.pojo.dto.system.LogSqlGroupByMethodDto;
import cn.org.donald.pojo.dto.system.QueryLogSqlByMethodDto;
import cn.org.donald.pojo.entity.system.LogSql;
import cn.org.donald.mapper.LogSqlMapper;
import cn.org.donald.pojo.vo.LogSqlGroupByMethodVo;
import cn.org.donald.service.system.LogSqlService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Donald
 * @since 2021-01-09
 */
@Service(version = "1.0.0", group = "donald")
public class LogSqlServiceImpl extends ServiceImpl<LogSqlMapper, LogSql> implements LogSqlService {

    private static Logger logger = LoggerFactory.getLogger(LogSqlServiceImpl.class);

    @Autowired
    private LogSqlMapper logSqlMapper;

    @Override
    public boolean addSqlLog(LogSql logSql) {
        int rows = logSqlMapper.insert(logSql);
        if (rows < 1) {
            logger.error("日志写入失败! {}", logSql.getMethod());
            return false;
        }
        return true;
    }

    @Override
    public IPage<LogSqlGroupByMethodVo> selectPageGroup(LogSqlGroupByMethodDto logSqlGroupByMethodDto) {
        return logSqlMapper.selectListGroupByMethod(logSqlGroupByMethodDto);
    }

    /**
     * 根据服务名称和mapper方法名查询sql日志
     *
     * @param queryLogSqlByMethodDto
     * @return
     */
    @Override
    public IPage<LogSql> selectByApplicationAndMethod(QueryLogSqlByMethodDto queryLogSqlByMethodDto) {
        QueryWrapper<LogSql> wrapper = new QueryWrapper<>();
        wrapper.eq("method",queryLogSqlByMethodDto.getMethod()).eq("application",queryLogSqlByMethodDto.getApplication());
        wrapper.orderByDesc("id");
        return logSqlMapper.selectPage(queryLogSqlByMethodDto, wrapper);
    }
}
